package com.shopping.service.impl;

import java.util.List;
import java.util.Map;

import com.shopping.bean.Goods;
import com.shopping.bean.Page;
import com.shopping.dao.GoodsDao;
import com.shopping.dao.impl.GoodsDaoImpl;
import com.shopping.service.GoodsService;

public class GoodsServiceImpl implements GoodsService {

	GoodsDao goodsDao = new GoodsDaoImpl();
	
	@Override
	public Page<Goods> getGoods(String keyword, int pageNum, int pageSize) {
		List<Goods> data = goodsDao.selectGoodsByKeyword(keyword, pageNum, pageSize);
		int totalRows = goodsDao.selectTotalRows(keyword);
		Page<Goods> page = new Page<Goods>(data,pageNum,pageSize,totalRows);
		return page;
	}

	@Override
	public List<Goods> selectGoos(String gType,int pageNum,int pageSize) {
		return goodsDao.selectGoods(gType,pageNum,pageSize);
	}

	@Override
	public Map<String, Object> findGoodsByName(String name) {
		return goodsDao.selectByName(name);
		
	}
	
	@Override
	public List<Goods> findAll() {
		return null;
	}
	
	@Override
	public int addGoods(Goods goods) {
		return goodsDao.insertGoods(goods);
	}
	
	@Override
	public String insertGoodsGoShopCar(int userId,int goodsId,int goodsNum) {
		Goods goods = goodsDao.selectGoodsById(goodsId);
		int num = goodsDao.insertGoodsOnShopCar(userId,goods,goodsNum);
		if(num == 1) {
			return "添加成功";
		}
		return "添加失败";
	}
	
	@Override
	public Page<Goods> findGoods(Map<String,String[]> condition) {
		
		List<Goods> list = goodsDao.selectGoods(condition);
		
		int pageSum = goodsDao.selectTotalRows(condition);
		int pageNum = 1;
		int pageSize = 5;
		
		try {
			pageNum = Integer.parseInt(condition.get("pageNum")[0]);
		}catch(Exception o) {
			o.printStackTrace();
		}
		try {
			pageSize = Integer.parseInt(condition.get("pageSize")[0]);
		}catch(Exception o) {
			o.printStackTrace();
		}
		Page<Goods> page = new Page<Goods>(list,pageNum,pageSize,pageSum);
		return page;
	}
	
	@Override
	public int changeGoods(Goods newGoods, Goods oldGoods) {
		// TODO Auto-generated method stub
		return goodsDao.updateGoods(newGoods, oldGoods);
	}

	@Override
	public int delGoodsById(int goodsId) {
		// TODO Auto-generated method stub
		return goodsDao.deleteGoodsById(goodsId);
	}

	@Override
	public Goods selectGoodsById(int gId) {
		// TODO Auto-generated method stub
		return goodsDao.selectGoodsById(gId);
	}
}
